<?php

namespace app\model;

use Logger;

class Model
{
  public $db;
  public $cms;
  public $s;
  public $m;
  public $c;
  public $id;
  public $t_path;
  public $title;
  public $keywords;
  public $description;

  function __construct()
  {
    $this->db = &$GLOBALS['db'];
    $this->cms = &$GLOBALS['cms'];
  }

  function return_json($res)
  {
    die(is_array($res) ? json_encode($res, 320) : $res);
  }

  function __call($name, $arguments)
  {
    die($GLOBALS['lang']['404']);
  }

  function csrf_token($mode = 0)
  {
    if (empty($_SESSION['csrf_token']) || $mode) {
      $_SESSION['csrf_token'] = bin2hex(str_rand(16));
    }

    return $_SESSION['csrf_token'];
  }

  function csrf()
  {
    return '<input type="hidden" name="csrf_token" value="' . $this->csrf_token() . '"/>';
  }

  function check_csrf()
  {
    if ($_SESSION['csrf_token'] === $_POST['csrf_token']) {
      $this->csrf_token(1);
      return true;
    }

    alert_back($GLOBALS['lang']['illegal']);
  }

  // 前端模板文件引入
  function tpl($tpl = '', $arr = [])
  {
    if ([] != $arr) {
      foreach ($arr as $key => $val) {
        ${$key} = $val;
      }
    }

    $this->s = iif(str_safe($_GET['s']), DEFAULT_MODULE);
    $this->m = iif(str_safe($_GET['m']), 'index');
    $this->c = iif(str_safe($_GET['c']), 'index');
    $this->id = iif(intval($_REQUEST['id']), 0);

    $this->title = !empty($GLOBALS['cms']['seoname']) ? $GLOBALS['cms']['name'] . '-' . $GLOBALS['cms']['seoname'] : $GLOBALS['cms']['name'];
    $this->keywords = str_text($GLOBALS['cms']['keywords'], 1);
    $this->description = str_cut(str_text($GLOBALS['cms']['description'], 1), 220);

    $GLOBALS['t_path'] = MODULE_DIR . '/' . $this->s . '/' . $GLOBALS['t_path'] . '/';

    if ('' != $tpl) {
      include $GLOBALS['t_path'] . get_file_name($tpl) . '.' . TPL_EX;
      return;
    }

    if (is_file($GLOBALS['t_path'] . $this->m . '.' . TPL_EX)) {
      include $GLOBALS['t_path'] . $this->m . '.' . TPL_EX;
      return;
    }

    die($GLOBALS['lang']['tpl_error']);
  }

  /**
   * data_ops
   * @param mixed $data [data_str,data_dir,data_name,log_type:[admin,sys],log_msg,redir_msg,redir_type:[1:href,0:back],redir_url]
   * @return never
   */
  function file_ops($data)
  {
    file_it($data['data_str'], $data['data_dir'], $data['data_name']);
    $this->common_ops($data);
  }

  /**
   * data_ops
   * @param mixed $data [db,sql,log_type:[admin,sys],log_msg,redir_msg,redir_type:[1:href,0:back],redir_url]
   * @return never
   */
  function data_ops($data)
  {
    $this->data_action($data['sql'], $data['db']);
    $this->common_ops($data);
  }

  function common_ops($data)
  {
    switch ($data['log_type']) {
      case 'admin':
        admin_log($data['log_msg']);
        break;
      case 'sys':
        Logger::log('sys_log', $data['log_msg']);
        break;
      default:
        break;
    }

    return !empty($data['redir_msg']) ?
      $this->alert_href_or_back($data['redir_type'], $data['redir_msg'], iif($data['redir_url'], '')) :
      $this->href_or_back($data['redir_type'], iif($data['redir_url'], ''));
  }

  function data_action($sql, $db = '')
  {
    $db = iif($db, $GLOBALS['db']);
    return $db->query($sql);
  }

  function href_or_back($type = 1, $url = '')
  {
    if ($type) href($url);
    back();
  }

  function alert_href_or_back($type = 1, $msg = '', $url = '')
  {
    if ($type) alert_href($msg, $url);
    alert_back($msg);
  }

  function __destruct()
  {
    unset($this->db);
    unset($this->cms);
    unset($this->s);
    unset($this->m);
    unset($this->c);
    unset($this->id);
    unset($this->t_path);
    unset($this->title);
    unset($this->keywords);
    unset($this->description);
  }
}